package com.one.ui;

import com.one.component.BackGroundPanel;
import com.one.util.CheckCodeFromMysql;
import com.one.util.ConfirmPassword;
import com.one.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;

public class RegistrationPage {
    JFrame jFrame = new JFrame("注册账号");

    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images\\regist.jpg").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;

    JLabel name = new JLabel("用  户  名：");
    JLabel password = new JLabel("密        码：");
    JLabel confirmPassword = new JLabel("确认密码：");
    JLabel gender = new JLabel("性       别:");

    JTextField nameTextField = new JTextField(15);
    JTextField passwordTextField = new JTextField(15);
    JTextField confirmTextField = new JTextField(15);

    JRadioButton male = new JRadioButton("男");
    JRadioButton female = new JRadioButton("女");

    JButton registered = new JButton("注册");
    JButton back = new JButton("返回登录界面");

    Connection connection;

    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth()-WIDTH) / 2,
                (ScreenUtils.getScreenHeight()-HEIGHT) / 2, WIDTH, HEIGHT);
        jFrame.setIconImage(new ImageIcon("images//logo.png").getImage());

        Box nameBox = Box.createHorizontalBox();
        Box passwordBox = Box.createHorizontalBox();
        Box confirmBox = Box.createHorizontalBox();
        Box genderBox = Box.createHorizontalBox();
        Box btnBox = Box.createHorizontalBox();

        nameBox.add(name);
        nameBox.add(Box.createHorizontalStrut(30));
        nameBox.add(nameTextField);

        passwordBox.add(password);
        passwordBox.add(Box.createHorizontalStrut(30));
        passwordBox.add(passwordTextField);

        confirmBox.add(confirmPassword);
        confirmBox.add(Box.createHorizontalStrut(30));
        confirmBox.add(confirmTextField);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(male);
        buttonGroup.add(female);

        genderBox.add(gender);
        genderBox.add(Box.createHorizontalStrut(30));
        genderBox.add(male);
        genderBox.add(female);

        btnBox.add(Box.createHorizontalStrut(30));
        btnBox.add(registered);
        btnBox.add(Box.createHorizontalStrut(180));
        btnBox.add(back);

        Box content = Box.createVerticalBox();
        content.add(Box.createVerticalStrut(80));
        content.add(nameBox);
        content.add(Box.createVerticalStrut(30));
        content.add(passwordBox);
        content.add(Box.createVerticalStrut(30));
        content.add(confirmBox);
        content.add(Box.createVerticalStrut(30));
        content.add(genderBox);
        content.add(Box.createVerticalStrut(60));
        content.add(btnBox);

        backGroundPanel.add(content);



//        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));


        jFrame.add(backGroundPanel);

//        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理
        registered.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newName = nameTextField.getText().trim();
                String newPassword = passwordTextField.getText().trim();
                String confirmPassword = confirmTextField.getText().trim();
                String userGender = male.isSelected() ? "男" : "女";
                System.out.println(userGender);

                System.out.println(ConfirmPassword.confirmTowPasswordIsSame(newPassword, confirmPassword));

                if (!ConfirmPassword.confirmTowPasswordIsSame(newPassword, confirmPassword)) {
                    JOptionPane.showMessageDialog(jFrame, "两次密码输入不一致！");
                } else if (CheckCodeFromMysql.isUserNameExits(newName)) {
                    JOptionPane.showMessageDialog(jFrame, "用户名已存在！");
                } else {

                    PreparedStatement sql;
                    try {
                        connection = DriverManager.getConnection(CheckCodeFromMysql.getUrl(), "root", "123456");
                        sql =connection.prepareStatement("insert into user_data values (?, ?, ?)");
                        sql.setString(1, newName);
                        sql.setString(2, newPassword);
                        sql.setString(3, userGender);
                        sql.execute();
                        JOptionPane.showMessageDialog(jFrame, "注册成功，即将跳转至登录界面。");
                        jFrame.dispose();
                        new MainInterface().init();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jFrame.dispose();
                new MainInterface().init();
            }
        });
    }
}
